package com.zbw.controller;

import com.zbw.domain.Message;
import com.zbw.domain.User;
import com.zbw.domain.Evaluate;
import com.zbw.service.IEvaluateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class evaluateController {

    @Resource
    private IEvaluateService evaluateService;

    @RequestMapping("/addEvaluate")
    @ResponseBody
    public String addEvaluate(Evaluate evaluate, HttpServletRequest request) {
        //获取当前登陆人的留言信息
        Evaluate evaluate1 = new Evaluate();
        User user = (User) request.getSession().getAttribute("user");
        evaluate1.setText(evaluate.getText());
        evaluate1.setBookId(evaluate.getBookId());
        evaluate1.setAddUser(user.getUserName());
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        evaluate1.setTime(format.format(date));
        evaluateService.insertEvaluate(evaluate1);

        return "true";
    }

    @RequestMapping("/showEvaluate")
    public String showEvaluate(Model model, HttpServletRequest request) {
        //获取当前登陆人的留言信息
//        User user = (User) request.getSession().getAttribute("user");
        List<Evaluate> list = evaluateService.queryEvaluate();
        model.addAttribute("evaluateList", list);
        return "user/evaluate";
    }

    /**
     * 返回用户评价
     */
    @RequestMapping("/evaluate")
    public String evaluate(String bookId,Model model,HttpServletRequest request) {
        model.addAttribute("bookId",bookId);
        return "user/evaluate";
    }

}
